import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '@/store/index';

Vue.use(VueRouter);

const routes = [
    {
        //控制面板
        path: '/',
        component: resolve => {
            require(['./components/panel/index'], resolve);
        },
        children: [
            {
                path: '',
                component: resolve => {
                    require(['./components/dashboard/index'], resolve);
                },
            },
            {
                path: 'dashboard',
                component: resolve => {
                    require(['./components/dashboard/index'], resolve);
                },
            },
            {
                path: 'shop',
                component: resolve => {
                    require(['./components/shop/list'], resolve);
                },
            },
            {
                path: 'shop/create',
                component: resolve => {
                    require(['./components/shop/create'], resolve);
                },
            },
            {
                path: 'shop/edit/:id',
                component: resolve => {
                    require(['./components/shop/edit'], resolve);
                },
            },
            {
                path: 'item',
                component: resolve => {
                    require(['./components/item/list'], resolve);
                },
            },
            {
                path: 'item/create',
                component: resolve => {
                    require(['./components/item/create'], resolve);
                },
            },
            {
                path: 'item/edit/:id',
                component: resolve => {
                    require(['./components/item/edit'], resolve);
                },
            },
            {
                path: 'user',
                component: resolve => {
                    require(['./components/user/list'], resolve);
                },
            },
            {
                path: 'order',
                component: resolve => {
                    require(['./components/order/list'], resolve);
                },
            },
            {
                path: 'category',
                component: resolve => {
                    require(['./components/category/index'], resolve);
                },
            },
            {
                path: 'log',
                component: resolve => {
                    require(['./components/panel/log'], resolve);
                },
            }
        ]
    },
    {
        path: '/login',
        component: resolve => {
            require(['./components/index'], resolve);
        },
    }
];

const router = new VueRouter({
    routes,
});

router.beforeEach(async (to, from, next) => {
    let token = localStorage.getItem('token') || '';
    if (to.path !== '/login' && !store.getters.token && !token) {
        return next('/login')
    }
    return next();
});

export default router;
